package com.cw.serviceimpl;

import com.cw.data.PageBean;
import com.cw.mapper.TbBrandMapper;
import com.cw.mapper.TbSpecificationMapper;
import com.cw.mapper.TbTypeTemplateMapper;
import com.cw.pojo.TbBrand;
import com.cw.pojo.TbSpecification;
import com.cw.pojo.TbTypeTemplate;
import com.cw.service.TemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Override
    public PageBean<TbTypeTemplate> getTemplateInfo(Integer pageNumber, Integer pageSize, String name) {

        PageHelper.startPage(pageNumber, pageSize);

        List<TbTypeTemplate> templateList = tbTypeTemplateMapper.getTemplateInfo(name);

        PageInfo<TbTypeTemplate> pa = new PageInfo<>(templateList);

        return new PageBean<TbTypeTemplate>(pa.getPageNum(), pa.getPageSize(), pa.getPages(), pa.getTotal(), pa.getList());
    }

    @Override
    public List<TbBrand> getBrandList() {
        return tbBrandMapper.selectByExample(null);
    }

    @Override
    public List<TbSpecification> getSpecList() {
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    @Transactional
    public void addTemplate(TbTypeTemplate tt) {

        tbTypeTemplateMapper.insertSelective(tt);

    }

    @Override
    public TbTypeTemplate getOneTemplate(Long id) {
        return tbTypeTemplateMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateTemplate(TbTypeTemplate tt) {

        tbTypeTemplateMapper.updateByPrimaryKeySelective(tt);
    }

    @Override
    @Transactional
    public void deleteTemplate(Long id) {

        tbTypeTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll(Long[] ids) {

        tbTypeTemplateMapper.deleteAll(ids);
    }
}
